
#define DISPTSK_C

#include "Global.h"



#pragma pack(1)
typedef struct _TYPE_DISP_TASK
{
	unsigned char		Index;
	unsigned int		LastTime;
	unsigned char		Blink;
	void (*DispFunc)(void);	
	void (*DispIconFunc)(void);
}TYPE_DISP_TASK;
#pragma pack()

unsigned short  DispLastT = 0;
unsigned char   DispIndex = 0;
unsigned char   DispBlink = 0;
unsigned char   DispMask = 0;
unsigned char   GobalBlink = 0;

unsigned char   DispBuf[TOTAL_COM] = {0};

#define	NumA					10
#define	NumB					11
#define	NumC					12
#define NumD					13
#define NumLine					16
#define NumBLANK				17
#define NumE					14
#define NumP					18
#define NumF					15
#define NumL					19
#define NumH					20
#define NumT					21
#define NumO					22
#define NumN					23
#define Numi					24
#define NumR					25
#define NumJ					26
#define Numu					27
#define NumG					28

unsigned char D8Tbl[] =
{
//---------------------------------------
//			A											
//		   ----
//		   |     |
//		F | G  | B
//		   ---- 										
//		   |     |
//		E |     | C
//		   ---- 										
//		   D	. H 
//---------------------------------------
// H,G,F,E_D,C,B,A
	b0011_1111,		// 0		//0
	b0000_0110,		// 1		//1
	b0101_1011,		// 2		//2
	b0100_1111, 	// 3		//3
	b0110_0110, 	// 4		//4
	b0110_1101, 	// 5		//5
	b0111_1101, 	// 6		//6
	b0000_0111, 	// 7		//7
	b0111_1111, 	// 8		//8
	b0110_1111, 	// 9		//9
	b0111_0111, 	// A		//10
	b0111_1100, 	// b		//11
	b0011_1001, 	// C		//12
	b0101_1110, 	// d		//13
	b0111_1001,		// E		//14
	b0111_0001, 	// F		//15
	b0100_0000, 	// -		//16
	b0000_0000, 	//BLANK	//17
	b0111_0011, 	//P 		//18
	b0011_1000,		//L		//19
	b0111_0110,		//H		//20
	b0011_0001,		//T		//21
	b0101_1100,		//0		//22
	b0101_0100,		//n		//23
	b0000_0100,		//i		//24
	b0101_0000,		//r		//25
	b0000_1110,		//J		//26
	b0001_1100,		//u		//27
	b0011_1101,		//G		//28
};

#define		COM_TEN			4  //  //  �� 8 ��С����
#define		COM_ONE			3  // // �� 8 �� %
#define		COM_POINT		2  //  С 8  C 
#define		COM_LED			1 // wifi  down line ptc ��Ҷ
#define		COM_TIME_HOME	0 // time  home

// power,angle,rl,updown,eco,maxmin,time

#define 	LED_AI			b0000_0100
#define 	LED_COOL		b0000_0010
#define 	LED_HEAT		b0001_0000
#define 	LED_DRY			b0000_0001
#define 	LED_FAN			b0000_1000


#define 	LED_PERCENT		BIT7
#define 	LED_C			BIT7
// COM 1
#define 	LED_WIFI		BIT5
#define 	LED_PTC			BIT6
#define 	LED_JINGHUA		BIT7

// COM 0
#define 	LED_TIME		BIT0
#define 	LED_AIR			BIT1



extern const TYPE_DISP_TASK DispTskTbl[DISP_TOTAL_TASK];


void Disp2Num(s16 temp)
{
	if(temp >= 0)
	{
		if(temp >= 99)
		{
			DispBuf[COM_TEN] |= D8Tbl[9];
			DispBuf[COM_ONE] |= D8Tbl[9];
		}
		else if(temp >= 10)
		{
			DispBuf[COM_TEN] |= D8Tbl[temp / 10];
			DispBuf[COM_ONE] |= D8Tbl[temp %10];
		}
		else
		{	
			DispBuf[COM_ONE] |= D8Tbl[temp];
		}
	}
	else
	{
		if(temp >= (-9))
		{
			DispBuf[COM_TEN] |= D8Tbl[NumLine];
			temp = 0 - temp;
			DispBuf[COM_ONE] |= D8Tbl[temp];
		}
		else
		{	
			DispBuf[COM_TEN] |= D8Tbl[NumLine];
			DispBuf[COM_ONE] |= D8Tbl[9];
		}
	}
}

void Disp3Num(s16 temp)
{
	if(temp >= 0)
	{
		if(temp >= 999)
		{
			DispBuf[COM_TEN] |= D8Tbl[9];
			DispBuf[COM_ONE] |= D8Tbl[9];
			DispBuf[COM_POINT] |= D8Tbl[9];
		}
		else if(temp >= 100)
		{
			DispBuf[COM_TEN] |= D8Tbl[temp / 100];
			DispBuf[COM_ONE] |= D8Tbl[(temp / 10)%10];
			DispBuf[COM_POINT] |= D8Tbl[temp % 10];
		}
		else
		{	
			DispBuf[COM_ONE] |= D8Tbl[temp / 10];
			DispBuf[COM_POINT] |= D8Tbl[temp % 10];
		}
	}
	else
	{
		if(temp >= (-99))
		{
			DispBuf[COM_TEN] |= D8Tbl[NumLine];
			temp = 0 - temp;
			DispBuf[COM_ONE] |= D8Tbl[temp/10];
			DispBuf[COM_POINT] |= D8Tbl[temp % 10];
		}
		else
		{	
			DispBuf[COM_TEN] |= D8Tbl[NumLine];
			DispBuf[COM_ONE] |= D8Tbl[9];
			DispBuf[COM_POINT] |= D8Tbl[9];
		}
	}
	
	//DispBuf[COM_TEN] |= LED_PERCENT;
}

void Disp3Hex(u16 temp)
{
	DispBuf[COM_TEN] |= D8Tbl[(temp >> 8) & 0x000f];
	DispBuf[COM_ONE] |= D8Tbl[(temp >> 4) & 0x000f];
	DispBuf[COM_POINT] |= D8Tbl[temp & 0x000f];
}

void DispAngle()
{

}

void DispSetTemp()
{

}


void DispTime()
{
	
}

void DispSelfTest()
{
	
}

uchar DispErr(void)
{
#ifndef DBG_NO_ERROR
	if(SysInfo.fg.OutComErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
	else if(SysInfo.fg.InFanErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[3];
    }
	else if(SysInfo.fg.OutEEPromErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[9];
    }
    else if(SysInfo.fg.T1Err)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
    else if(SysInfo.fg.T2Err)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[2];
    }
	else if(SysInfo.fg.T3Err)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[3];
    }
    else if(SysInfo.fg.T4Err)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[4];
    }
	else if(SysInfo.fg.TpErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[5];
    }
    else if(SysInfo.fg.DispComErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[NumC];
    }
    else if(SysInfo.fg.FreshAirErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumE];
        DispBuf[COM_ONE] |= D8Tbl[6];
    }
	else if(SysInfo.fg.HumSensorErr)
	{
        DispBuf[COM_TEN] |= D8Tbl[NumF];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
    else
	{
		return 0;
    }
	return 1;
#else
    return 0;
#endif
}

uchar DispEngineeErr(void)
{
	if(SysInfo.fg.T2Protect)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[2];
    }
	else if(SysInfo.fg.T3Protect)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[3];
    }
	else if(SysInfo.fg.TpProtect)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[5];
    }
    else if(SysInfo.fg.IPMHiTempPrt)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[6];
    }
    else if(SysInfo.fg.TestTpErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[8];
    }
	 else if(SysInfo.fg.TestT3Err)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[2];
		DispBuf[COM_POINT] |= D8Tbl[9];
    }
    else if(SysInfo.fg.Pressure2High)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[3];
		DispBuf[COM_POINT] |= D8Tbl[0];
    }
	else if(SysInfo.fg.Pressure2Low)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumP];
        DispBuf[COM_ONE] |= D8Tbl[3];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
   	else if(SysInfo.fg.PFCurrentCover)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumH];
        DispBuf[COM_ONE] |= D8Tbl[3];
		DispBuf[COM_POINT] |= D8Tbl[2];
    }
	else if(SysInfo.fg.VoltLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
	else if(SysInfo.fg.T2FreLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[2];
    }
	else if(SysInfo.fg.T3FreLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[3];
    }
	else if(SysInfo.fg.TpFreLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[5];
    }
	else if(SysInfo.fg.IPMHiTempLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[6];
    }
	else if(SysInfo.fg.CurrentLimit)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumL];
        DispBuf[COM_ONE] |= D8Tbl[0];
		DispBuf[COM_POINT] |= D8Tbl[NumC];
    }
	else if(SysInfo.fg.HumSensorErr)
    {
        DispBuf[COM_TEN] |= D8Tbl[NumF];
        DispBuf[COM_ONE] |= D8Tbl[1];
		DispBuf[COM_POINT] |= D8Tbl[1];
    }
    else
	{
		DispBuf[COM_TEN] |= D8Tbl[NumN];
        DispBuf[COM_ONE] |= D8Tbl[NumA];
		return 0;
	}
	return 1;
}

void DispNormalOther(void)
{
	if(SysSet.fg.TurnOn)
	{
		if(SysSet.Mode == MODE_HEAT)
		{
			DispBuf[COM_LED] |= LED_HEAT;
			if(SysSet.fg.PTC)DispBuf[COM_LED] |= LED_PTC;
			DispBuf[COM_POINT] |= LED_C;
		}
		else if(SysSet.Mode == MODE_COOL)
		{
			DispBuf[COM_LED] |= LED_COOL;
			DispBuf[COM_POINT] |= LED_C;
		}
		else if(SysSet.Mode == MODE_DRY)
		{
			DispBuf[COM_LED] |= LED_DRY;
			DispBuf[COM_ONE] |= LED_PERCENT;
		}
		else if(SysSet.Mode == MODE_FAN)
		{
			DispBuf[COM_LED] |= LED_FAN;
			DispBuf[COM_POINT] |= LED_C;
		}
		else
		{
			DispBuf[COM_LED] |= LED_AI;
			DispBuf[COM_POINT] |= LED_C;
		}
		if(SysSet.fg.Ion)DispBuf[COM_LED] |= LED_JINGHUA;
		if(SysSet.fg.FreshAir)DispBuf[COM_TIME_HOME] |= LED_AIR;
		if(SysSet.fg.TimeOff)DispBuf[COM_TIME_HOME] |= LED_TIME;
	}
	else
	{
		if(SysSet.fg.TimeOn)DispBuf[COM_TIME_HOME] |= LED_TIME;
	}
}


void DispNormal(void)
{
	if(SysSet.fg.TurnOn)
	{
		if(DispErr())
		{

		}
		else if(SysSet.fg.ECO)
		{
			DispBuf[COM_TEN] |= D8Tbl[NumE];
			DispBuf[COM_ONE] |= D8Tbl[NumC];
			DispBuf[COM_POINT] |= D8Tbl[0];
		}
		else if(SysSet.Mode == MODE_HEAT)
		{
			Disp3Num(SysSet.Temp_C);
		}
		else if(SysSet.Mode == MODE_COOL)
		{
			Disp3Num(SysSet.Temp_C);
		}
		else if(SysSet.Mode == MODE_DRY)
		{
			Disp2Num(SysSet.Hum);
		}
		else if(SysSet.Mode == MODE_FAN)
		{
			Disp3Num(SysInfo.T1);
		}
		else
		{
			Disp3Num(SysSet.Temp_C);
		}
	}
	else
	{
	
	}
}

void DispSpecilMode()
{	
	switch(SysSet.CheckChannel)
	{
		case 0:
			if(DispErr() == 0)
			{
				DispEngineeErr();	
			}
			break;

		case 1:
			Disp3Num(SysInfo.T1);
			DispBuf[COM_POINT] |= LED_C;
			break;

		case 2:
			Disp3Num(SysInfo.T4);
			DispBuf[COM_POINT] |= LED_C;
			break;

		case 3:
			Disp3Num(SysInfo.T2);
			DispBuf[COM_POINT] |= LED_C;
			break;

		case 4:
			Disp3Num(SysInfo.T3);
			DispBuf[COM_POINT] |= LED_C;
			break;

		case 5:
			Disp3Num(SysInfo.TP);
			DispBuf[COM_POINT] |= LED_C;
			break;

		case 6:
			Disp3Num(OutDrv.CompFre);
			break;

		case 7:
			Disp3Num(SysInfo.CompFre);
			break;

		case 8:
			Disp3Num(SysInfo.CompFre);
			break;

		case 9:
			Disp3Num(SysInfo.InFanCurSpeed >> 3);
			break;

		case 10:
			Disp3Num(SysInfo.OutFanCurSpeed >> 3);
			break;

		case 11:
			Disp3Hex(SysInfo.EEVStep);
			break;

		case 12:
			DispBuf[COM_TEN] |= D8Tbl[NumN];
			DispBuf[COM_ONE] |= D8Tbl[NumA];
			break;

		case 13:
			DispBuf[COM_TEN] |= D8Tbl[NumN];
			DispBuf[COM_ONE] |= D8Tbl[NumA];
			break;

		case 14:
			Disp2Num(SysSet.Hum);
			//DispBuf[COM_ONE] |= LED_PERCENT;
			break;

		case 15:
			Disp3Num(SysInfo.SetTemp - SysSet.Temp_C);
			DispBuf[COM_ONE] |= LED_C;
			break;

		case 16:
			//Disp3Hex(SysInfo.NewFanFilterPercent);
			break;

		default:
			DispBuf[COM_TEN] |= D8Tbl[NumN];
			DispBuf[COM_ONE] |= D8Tbl[NumA];
			break;
	}
}

void DispPowerUpOther()
{
	DispBuf[COM_LED] |= 0xff;
	DispBuf[COM_TIME_HOME] |= 0xff;
}

void DispPowerUp()
{
	DispBuf[COM_TEN] |= D8Tbl[8];
	DispBuf[COM_ONE] |= D8Tbl[8];
	DispBuf[COM_POINT] |= D8Tbl[8];
}

void DispMachineType()
{
	DispBuf[COM_TEN] |= D8Tbl[3];
	DispBuf[COM_ONE] |= D8Tbl[5];
}

void DispClean()
{
	SetStr(DispBuf,0,TOTAL_COM);
}

void SegSet(uchar buf)
{

}

/**************************************************************
//Fuction Name: UpdateDispBuff(void)
//Desrition:
//Input:  
//Output:
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
uchar ConverDisp(uchar buf)
{
	return 0;
}

/**************************************************************
//Fuction Name: UpdateDispBuff()
//Desrition:
//Input:  
//Output: 
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
void UpdateDispBuff(void)
{
	uchar ten,one;
	uint8 u8Tmp;
    uint8 type;

	static uint8 DispBack = 0;
	static uint8 DispUpdateTime = 1;

	for(ten = 0 ; ten < sizeof(D8Tbl); ten ++)
	{	
		if(DispBuf[COM_TEN] == D8Tbl[ten])break;
	}

	for(one = 0 ; one < sizeof(D8Tbl); one ++)
	{	
		if(DispBuf[COM_ONE] == D8Tbl[one])break;
	}

	if((NumBLANK == ten)&&(NumBLANK == one))
	{
		type = 0;
		u8Tmp = 0xdd;
	}
	else
	{
		type = 1;
		if(ten == NumBLANK)
			u8Tmp = 0;
		else if(ten > 0x0f)
			u8Tmp = 0x0d;
		else
			u8Tmp = ten;

		u8Tmp = u8Tmp << 4;
		
		if(one == NumBLANK)
			u8Tmp |= 0;
		else if(one > 0x0f)
			u8Tmp |= 0x0d;
		else
			u8Tmp |= one;
	}
	
	if(DispUpdateTime)DispUpdateTime--;
	if((DispBack != u8Tmp)
	||(DispUpdateTime == 0))
	{
		DispBack = u8Tmp;
		DispUpdateTime = 50;
		UpdeteDisapBuf(u8Tmp,type);
	}

	if(SysSet.fg.TurnOn)
	{
		MR88FX02_Gpio_Set(PO_LED_POWER,0);
	}
	else
	{
		MR88FX02_Gpio_Set(PO_LED_POWER,1);
	}

	if(SysInfo.CompFre)
	{
		MR88FX02_Gpio_Set(PO_LED_COMP,0);
	}
	else
	{
		MR88FX02_Gpio_Set(PO_LED_COMP,1);
	}

	
	if(SysSet.fg.FreshAir)
	{
		MR88FX02_Gpio_Set(PO_LED_APS,0);
	}
	else
	{
		MR88FX02_Gpio_Set(PO_LED_APS,1);
	}
}

/**************************************************************
//Fuction Name: DispInit()
//Desrition:
//Input:  
//Output: 
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
void DispInit(void)
{
	DispTskChange(DIPS_POWERUP);
}

/**************************************************************
//Fuction Name: unsigned char GetDispType()
//Desrition:
//Input:  
//Output:
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
unsigned char GetDispType(void)
{
	return DispTskTbl[DispIndex].Index;
}

/**************************************************************
//Fuction Name: DispTskChange(unsigned char temp)
//Desrition:
//Input:  
//Output:
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
void DispTskChange(unsigned char temp)
{
	unsigned char i;
	for(i = 0; i < DISP_TOTAL_TASK; i ++)
	{
		if(DispTskTbl[i].Index == temp)
		{
			DispIndex = i;
			DispLastT = DispTskTbl[i].LastTime;
			DispBlink = DispTskTbl[i].Blink;
			DispMask = 0;
			GobalBlink = 0;
			return;
		}
	}
}

/**************************************************************
//Fuction Name: void DispTskProc()
//Desrition:
//Input:  
//Output:
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
void DispTskProc()
{
	if(DispLastT)
	{
		DispLastT --;
		if(DispLastT == 0)
		{
			DispTskChange(DIPS_NORMAL);
		}
	}
	if(DispBlink)
	{
		DispBlink --;
		if(DispBlink == 0)
		{
			DispBlink = DispTskTbl[DispIndex].Blink;
			DispMask = !DispMask;
		}
	}
	DispClean();
	if(DispMask == 0)
	{
		if(DispTskTbl[DispIndex].DispFunc != NULL)
		{
			(DispTskTbl[DispIndex].DispFunc)();
		}
		if(DispTskTbl[DispIndex].DispIconFunc != NULL)
		{
			(DispTskTbl[DispIndex].DispIconFunc)();
		}
	}	
}

/**************************************************************
//Fuction Name: DispProc(void)
//Desrition:
//Input:  
//Output: 
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
void DispProc(void)
{
	if(GetTimeTick(TIME_100ms))
	{
		DispTskProc();		// 虚拟LED灯
		UpdateDispBuff(); 	// 转换成实体LED灯
		GobalBlink ++;
	}
}

/**************************************************************
//Fuction Name: 显示模块事件表
//Desrition:
//Input:  
//Output: 
//Author:Chen shou min
//Date:2014.12.1
***************************************************************/
const TYPE_DISP_TASK DispTskTbl[DISP_TOTAL_TASK] = 
{
// Index,		LastTime,	Blink *100ms,	双88数码管显示，		周边图标显示
{DIPS_NORMAL, 		0,			0,			&DispNormal,		&DispNormalOther},
{DIPS_POWERUP, 		10,			0,			&DispPowerUp,		&DispPowerUpOther},
{DIPS_SELFTEST, 	100,		0,			&DispSelfTest,		NULL},
{DIPS_SPECIL,		600, 		5,			&DispSpecilMode,	NULL},
{DIPS_TIME, 		50,			5,			&DispTime,			&DispNormalOther},
{DIPS_SET_TEMP, 	50,			5,			&DispSetTemp,		&DispNormalOther},
{DIPS_ANGLE,		30, 		5,			&DispAngle,			&DispNormalOther},
{DIPS_TYPE, 		10, 		0,			&DispMachineType,	NULL},
{DIPS_OFF, 			0, 			0,			NULL,				NULL},

};

